/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.messaging.inbox;

import nl.cloudfarming.client.messaging.inbox.InboxRow;
import java.awt.Component;
import java.awt.event.FocusEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JLabel;
import junit.framework.Assert;
import nl.cloudfarming.client.messaging.Message;
import org.junit.Test;
import org.uispec4j.UISpec4J;
import org.uispec4j.Panel;
import org.uispec4j.finder.ComponentMatcher;

/**
 *
 * @author Merijn Zengers
 */
public class InboxRowIT {

    private final JLabelComponentMatcher jlabelRowComponentMatcher = new JLabelComponentMatcher();

    static {
        UISpec4J.init();
    }

    @Test
    public void testContent() {
        Date sendDate = new Date();

        Message.Builder messageBuilder = new Message.Builder();

        messageBuilder.setOrigin("From").setTitle("subject").setDateReceived(sendDate);

        InboxRow instance = new InboxRow(messageBuilder.build());

        Panel panel = new Panel(instance);

        Assert.assertNotNull(panel.getSwingComponents(jlabelRowComponentMatcher));

        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        String date = sdf.format(sendDate);

        for (Component comp : panel.getSwingComponents(jlabelRowComponentMatcher)) {
            JLabel label = (JLabel) comp;
            Assert.assertNotNull(label.getText());
            Assert.assertTrue(label.getText().equals("From") || label.getText().equals("subject") || label.getText().equals(date));
        }
    }

    /**
     * Test of focusGained method, of class InboxRow.
     */
    @Test
    public void testFocusGained() {
        Message.Builder messageBuilder = new Message.Builder();

        messageBuilder.setOrigin("From").setTitle("subject").setDateReceived(new Date());
        InboxRow instance = new InboxRow(messageBuilder.build());
        FocusEvent e = new FocusEvent(instance, 0);

        instance.focusGained(e);

        Assert.assertTrue(instance.isSelected());
    }

    /**
     * Test of focusLost method, of class InboxRow.
     */
    @Test
    public void testFocusLost() {

        InboxRow instance = new InboxRow(new Message.Builder().setDateReceived(new Date()).build());
        FocusEvent e = new FocusEvent(instance, 0);

        instance.focusLost(e);

        Assert.assertFalse(instance.isSelected());
    }

    class JLabelComponentMatcher implements ComponentMatcher {

        @Override
        public boolean matches(Component component) {
            return (component instanceof JLabel);
        }
    }
}
